package com.example.swenp;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import rfc.Master;

public class Connector {
	private ObjectOutputStream oos;
	private ObjectInputStream ois ;
	private SSLSocket socket;
	    
	
	public Connector(){
	try {
		XTrustProvider.install();
		//System.setProperty("javax.net.ssl.trustStore","myKeystore"); // Truststore File in den Umgebungsvariablen festlegen
		//System.setProperty("javax.net.ssl.trustStorePassword", "1234567"); // Truststore Passwort in den Umgebungsvariablen festlegen
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); // Erzeugt eine SSLSocketFactory mit Standartkonfiguration
        socket = (SSLSocket) factory.createSocket("141.69.199.36", 1337); // SSLSocket erstellen (und zu Server verbinden)
		oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
	
	public Master leseNachricht() throws IOException, ClassNotFoundException {

		Master m = (Master)ois.readObject();
		return m;
	}
	public void schreibeNachricht(Master m) throws IOException {
				
	    		oos.writeObject(m);
	}
	public void close(){
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}
